package cn.aran.pet.service.impl;

import cn.aran.pet.domain.PetType;
import cn.aran.pet.mapper.PetTypeMapper;
import cn.aran.pet.service.IPetTypeService;
import cn.aran.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-16
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {
    @Autowired
    private PetTypeMapper petTypeMapper;

    /**
    * @Title: save
    * @Description: 保存
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/18 15:43
    * @Parameters: [petType]
    * @Return void
    */
    @Transactional
    @Override
    public void save(PetType petType) {
        petTypeMapper.save(petType);
        // this.update(petType);
        String dirPath = "";
        if (petType.getParent().getId() == null){
            dirPath = "/" + petType.getId();
        }else {
            PetType parent = petTypeMapper.loadById(petType.getParent().getId());
            if(parent != null){
                dirPath = parent.getDirPath() + "/" + petType.getId();
            }
        }
        petType.setDirPath(dirPath);
        petTypeMapper.update(petType);
    }

    /**
    * @Title: update
    * @Description: 宠物类型修改
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/18 15:43
    * @Parameters: [petType]
    * @Return void
    */
    @Transactional
    @Override
    public void update(PetType petType) {
        String dirPath = "";
        if (petType.getParent().getId() == null){
            dirPath = "/" + petType.getId();
        }else {
            PetType parent = petTypeMapper.loadById(petType.getParent().getId());
            if(parent != null){
                dirPath = parent.getDirPath() + "/" + petType.getId();
            }
        }
        petType.setDirPath(dirPath);
        petTypeMapper.update(petType);
    }
    
    /**
    * @Title: loadTypeTree
    * @Description: 宠物类型树
    * @Author: Aran
    * @Version: 1.0
    * @Date:  2022/7/18 15:39
    * @Parameters: []
    * @Return java.util.List<cn.aran.pet.domain.PetType>
    */
    @Override
    public List<PetType> loadTypeTree() {
        List<PetType> allType = petTypeMapper.loadAll();
        Map<Long, PetType> allMap = allType.stream().collect(Collectors.toMap(PetType::getId, item -> item));
        List<PetType> petTypes = new ArrayList<>();
        for (PetType typeEmp : allType) {
            if (typeEmp.getPid() == null) {
                petTypes.add(typeEmp);
            } else {
                PetType petType = allMap.get(typeEmp.getPid());
                if (petType != null) {
                    petType.getChildren().add(typeEmp);
                }
            }
        }

        return petTypes;
    }
}
